package com.blockmeta.bbs.immodule.dao;

import android.text.TextUtils;

import com.blockmeta.bbs.immodule.widget.msgview.SuppotMsgViewType;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.Transient;

import java.io.Serializable;

/**
 * Created by author on 2019/2/13.
 * 聊天信息存储
 */
@Entity(indexes = {@Index(value = "uuid DESC, whoId DESC", unique = true)})
public class ChatMessage implements MultiItemEntity, Serializable {
    private static final long serialVersionUID = 1223345L;


    /**
     * 发送消息
     */
    public final static int TYPE_SEND = 0;
    /**
     * 接收消息
     */
    public final static int TYPE_RECEIVE = 1;


    public final static int MSG_TYPE_UU = 0;// 人人之间的消息
    public final static int MSG_TYPE_UCG = 1;// 群消息

    public final static int CONTENT_TYPE_NORMAL = 0;// 普通消息
    public final static int CONTENT_TYPE_ATTACHMENT = 1;// 为带附件消息
    public final static int CONTENT_TYPE_NOTIFY = 2;// 为通知消息
    public final static int CONTENT_TYPE_CUSTOM = 4;// 为自定义消息
    public final static int CONTENT_TYPE_HONGBAO = 5;// 为红包
    public final static int CONTENT_TYPE_NONOTIFY = 6;// 不需要弹出通知栏的通知

    //用于Status字段标示！
    public final static int STATUS_SENDING = -1;// 发送中
    public final static int STATUS_UNKNOWN = 0;// 未知状态
    public final static int STATUS_SEND = 1;// 默认状态已发送
    //    public final static int STATUS_RECEIVED = 2;// 已收到
//    public final static int STATUS_READ = 3;// 已阅读
    public final static int STATUS_FAIL_FRIEND_SHIELD = 4;// 发送失败，好友被屏蔽
    public final static int STATUS_FAIL_TEMP_SHIELD = 5;// 发送失败，临时关系被屏蔽
    public final static int STATUS_FAIL_NOT_MEMBER = 6;// 发送失败，不是圈子成员
    public final static int STATUS_FAIL_CHATGROUP_DISBAND = 9;// 发送失败，不是圈子成员
    public final static int STATUS_FAIL_RATE_LIMIT = 11;// 消息发送频率过快
    public final static int STATUS_FAIL_SERVER_CLOSE = 12;// 消息服务停用


    public final static int STATUS_FAIL_TIMEOUT = 99;// 发送超时

    public final static int IM_LIST_TAG = 0;
    public final static int IM_CHATDETAIL_TAG = 1;

    public static final int TYPE_UNKNOWN = 0;// 未知文件类型
    public static final int TYPE_JPG = 1;
    public static final int TYPE_PNG = 2;
    public static final int TYPE_GIF = 3;
    public static final int TYPE_DOC = 4;
    public static final int TYPE_DOCX = 5;
    public static final int TYPE_PPT = 6;
    public static final int TYPE_PPTX = 7;
    public static final int TYPE_XLS = 8;
    public static final int TYPE_XLSX = 9;
    public static final int TYPE_PDF = 10;
    public static final int TYPE_TXT = 11;
    public static final int TYPE_RAR = 12;
    public static final int TYPE_ZIP = 13;
    public static final int TYPE_VOICE = 14;// 语音
    public static final int TYPE_VIDEO = 15;// 视频


    public static final int CUSTOM_TYPE_F10 = 1;
    public static final int CUSTOM_TYPE_KLINE = 2;
    public static final int CUSTOM_TYPE_CARD = 3;
    public static final int CUSTOM_TYPE_FORUMPOST = 4;
    public static final int CUSTOM_TYPE_POST = 5;
    public static final int CUSTOM_TYPE_ACTIVITY = 6;
    public static final int CUSTOM_TYPE_GOODHING = 7;
    //标记页面的位置
    @Transient
    public int TAG_WHERE = 0;

    public void setTAG_WHERE(int TAG_WHERE) {
        this.TAG_WHERE = TAG_WHERE;
    }


    /**
     * 非持久化字段，用于计数还有多少未读消息
     */
    private int unCheckedCount;
    private String uuid;
    private int status;
    private String fileGroupName;
    private String filePath;
    // 普通消息,附件消息，通知消息
    private int contentType;// 消息类型

    private long chatMessageId;

    //当前用户
    private long whoId;

    private Boolean checked;
    @Id(autoincrement = true)
    private Long id;

    //其他用户
    private long fromId;

    private long toId;

    private String content;

    private int date;//时间采用int 本地采用int值

    /**
     * 消息类型，0--发送消息，1--接收消息
     */
    private int type;
    /**
     * 消息类型，0--人人消息 1--群消息
     */
    private int msgType;

    /**
     * 当msgtype为群消息时不为空
     * 用于区分群组ID
     */
    private long chatGroupId;

    /**
     * 当为讨论组消息时不为空
     */

    private long discussionGroupId;
    // 是否需要转发，如果传入的是clinet则需要转发，如果是服务端则说明本就是转发消息不需要转发
    private boolean transfer;

    //删除Attachment表，然后新增file_name，file_type
    private String fileName;

    //文件的类型
    private int fileType;

    private String fileParam;

    @Transient
    private String userName;

    @Transient
    private String userAvator;

    @Transient
    private int userGroupId;

    @Generated(hash = 1334556267)
    public ChatMessage(int unCheckedCount, String uuid, int status, String fileGroupName,
                       String filePath, int contentType, long chatMessageId, long whoId, Boolean checked, Long id,
                       long fromId, long toId, String content, int date, int type, int msgType, long chatGroupId,
                       long discussionGroupId, boolean transfer, String fileName, int fileType, String fileParam) {
        this.unCheckedCount = unCheckedCount;
        this.uuid = uuid;
        this.status = status;
        this.fileGroupName = fileGroupName;
        this.filePath = filePath;
        this.contentType = contentType;
        this.chatMessageId = chatMessageId;
        this.whoId = whoId;
        this.checked = checked;
        this.id = id;
        this.fromId = fromId;
        this.toId = toId;
        this.content = content;
        this.date = date;
        this.type = type;
        this.msgType = msgType;
        this.chatGroupId = chatGroupId;
        this.discussionGroupId = discussionGroupId;
        this.transfer = transfer;
        this.fileName = fileName;
        this.fileType = fileType;
        this.fileParam = fileParam;
    }


    @Generated(hash = 2271208)
    public ChatMessage() {
    }




    /**
     * 发送 接收消息类型！ 0发送 1接收  type
     * 群消息 人人消息 0 人人 1 群消息  msgType
     *
     * @return
     */
    @Override
    public int getItemType() {
        if (TAG_WHERE == IM_LIST_TAG) {//列表页面消息
            return msgType;
        } else {
            //返回值需要在本地支持的viewtype类型中
            int itemType = contentType;

            if (contentType == ChatMessage.CONTENT_TYPE_ATTACHMENT) {
                //附件jpg 为101,非图片附件暂时用4
                if ((fileType > TYPE_GIF && fileType <= TYPE_VIDEO) || fileType == TYPE_UNKNOWN) {
                    itemType = ChatMessage.CONTENT_TYPE_ATTACHMENT * 100 + ChatMessage.TYPE_DOC;
                } else if (fileType == TYPE_JPG || fileType == TYPE_PNG) {
                    itemType = ChatMessage.CONTENT_TYPE_ATTACHMENT * 100 + ChatMessage.TYPE_JPG;
                } else {
                    itemType = ChatMessage.CONTENT_TYPE_ATTACHMENT * 100 + fileType;
                }
            } else if (contentType == ChatMessage.CONTENT_TYPE_CUSTOM) {
                //自定义消息的类型放在filetype中
                if (fileType == 0) {
                    itemType = ChatMessage.CONTENT_TYPE_NORMAL;
                } else {
                    itemType = ChatMessage.CONTENT_TYPE_CUSTOM * 100 + fileType;
                }
            } else {
                itemType = contentType;
            }
            if (!SuppotMsgViewType.contain(itemType)) {
                itemType = SuppotMsgViewType.UNSUPPORT_KEY;
            }
            return itemType;
        }
    }


    public int getUnCheckedCount() {
        return this.unCheckedCount;
    }


    public String getUuid() {
        return this.uuid;
    }


    public int getStatus() {
        return this.status;
    }


    public String getFileGroupName() {
        return this.fileGroupName;
    }


    public String getFilePath() {
        return this.filePath;
    }


    public int getContentType() {
        return this.contentType;
    }


    public long getChatMessageId() {
        return this.chatMessageId;
    }


    public Boolean getChecked() {
        return this.checked;
    }


    public Long getId() {
        return this.id;
    }



    public String getContent() {
        return this.content;
    }


    public int getDate() {
        return this.date;
    }


    public int getType() {
        return this.type;
    }


    public int getMsgType() {
        return this.msgType;
    }


    public boolean getTransfer() {
        return this.transfer;
    }


    public String getFileName() {
        return this.fileName;
    }


    public int getFileType() {
        return this.fileType;
    }


    public String getFileParam() {
        return this.fileParam;
    }


    public String getUserName() {
        return this.userName;
    }


    public String getUserAvator() {
        return this.userAvator;
    }


    public void setUnCheckedCount(int unCheckedCount) {
        this.unCheckedCount = unCheckedCount;
    }


    public void setUuid(String uuid) {
        this.uuid = uuid;
    }


    public void setStatus(int status) {
        this.status = status;
    }


    public void setFileGroupName(String fileGroupName) {
        this.fileGroupName = fileGroupName;
    }


    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }


    public void setContentType(int contentType) {
        this.contentType = contentType;
    }


    public void setChecked(Boolean checked) {
        this.checked = checked;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public void setContent(String content) {
        this.content = content;
    }


    public void setDate(int date) {
        this.date = date;
    }


    public void setType(int type) {
        this.type = type;
    }


    public void setMsgType(int msgType) {
        this.msgType = msgType;
    }


    public void setTransfer(boolean transfer) {
        this.transfer = transfer;
    }


    public void setFileName(String fileName) {
        this.fileName = fileName;
    }


    public void setFileType(int fileType) {
        this.fileType = fileType;
    }


    public void setFileParam(String fileParam) {
        this.fileParam = fileParam;
    }


    public void setUserName(String userName) {
        this.userName = userName;
    }


    public void setUserAvator(String userAvator) {
        this.userAvator = userAvator;
    }

    public void setChatMessageId(long chatMessageId) {
        this.chatMessageId = chatMessageId;
    }

    public long getWhoId() {
        return whoId;
    }

    public void setWhoId(long whoId) {
        this.whoId = whoId;
    }

    public long getFromId() {
        return fromId;
    }

    public void setFromId(long fromId) {
        this.fromId = fromId;
    }

    public long getToId() {
        return toId;
    }

    public void setToId(long toId) {
        this.toId = toId;
    }

    public long getChatGroupId() {
        return chatGroupId;
    }

    public void setChatGroupId(long chatGroupId) {
        this.chatGroupId = chatGroupId;
    }

    public long getDiscussionGroupId() {
        return discussionGroupId;
    }

    public void setDiscussionGroupId(long discussionGroupId) {
        this.discussionGroupId = discussionGroupId;
    }

    public boolean isTransfer() {
        return transfer;
    }

    public int getUserGroupId() {
        return userGroupId;
    }

    public void setUserGroupId(int userGroupId) {
        this.userGroupId = userGroupId;
    }

    public static int getChatMessageFileType(String ext) {
        int fileType = TYPE_UNKNOWN;
        if (TextUtils.isEmpty(ext)) {
            return fileType;
        }
        String s = ext.toLowerCase();
        if ("jpg".equals(s)) {
            fileType = TYPE_JPG;
        } else if ("jpeg".equals(s)) {
            fileType = TYPE_JPG;
        } else if ("png".equals(s)) {
            fileType = TYPE_PNG;
        } else if ("gif".equals(s)) {
            fileType = TYPE_GIF;
        } else if ("doc".equals(s)) {
            fileType = TYPE_DOC;
        } else if ("docx".equals(s)) {
            fileType = TYPE_DOCX;
        } else if ("ppt".equals(s)) {
            fileType = TYPE_PPT;
        } else if ("pptx".equals(s)) {
            fileType = TYPE_PPTX;
        } else if ("xls".equals(s)) {
            fileType = TYPE_XLS;
        } else if ("xlsx".equals(s)) {
            fileType = TYPE_XLSX;
        } else if ("pdf".equals(s)) {
            fileType = TYPE_PDF;
        } else if ("txt".equals(s)) {
            fileType = TYPE_TXT;
        } else if ("rar".equals(s)) {
            fileType = TYPE_RAR;
        } else if ("zip".equals(s)) {
            fileType = TYPE_ZIP;
        } else if ("mp3".equals(s)) {
            fileType = TYPE_VOICE;
        } else if ("mp4".equals(s)) {
            fileType = TYPE_VIDEO;
        }
        return fileType;
    }


    @Override
    public String toString() {
        return "ChatMessage{" +
                "TAG_WHERE=" + TAG_WHERE +
                ", unCheckedCount=" + unCheckedCount +
                ", uuid='" + uuid + '\'' +
                ", status=" + status +
                ", fileGroupName='" + fileGroupName + '\'' +
                ", filePath='" + filePath + '\'' +
                ", contentType=" + contentType +
                ", chatMessageId=" + chatMessageId +
                ", whoId=" + whoId +
                ", checked=" + checked +
                ", id=" + id +
                ", fromId=" + fromId +
                ", toId=" + toId +
                ", content='" + content + '\'' +
                ", date=" + date +
                ", type=" + type +
                ", msgType=" + msgType +
                ", chatGroupId=" + chatGroupId +
                ", discussionGroupId=" + discussionGroupId +
                ", transfer=" + transfer +
                ", fileName='" + fileName + '\'' +
                ", fileType=" + fileType +
                ", fileParam='" + fileParam + '\'' +
                ", userName='" + userName + '\'' +
                ", userAvator='" + userAvator + '\'' +
                ", userGroupId=" + userGroupId +
                '}';
    }


}
